﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using Ext.Net;

namespace LongSpeed.WebSite.Main.Admin
{
    public partial class ArticleList : Behaviors.Session
    {

        private int PartId
        {
            get { return Convert.ToInt32(Request.QueryString["partId"]); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindArticleData();
            }
        }

        private IList<Behaviors.Article> _ArticleData = null;

        public IList<Behaviors.Article> ArticleData
        {
            get
            {
                if (_ArticleData == null)
                {
                    LongSpeed.WebSite.Behaviors.Article a = new LongSpeed.WebSite.Behaviors.Article();
                    _ArticleData = a.GetAllArticleByPart(this.PartId);
                }

                return _ArticleData;
            }
            set { _ArticleData = value; }
        }

        protected void RefreshArticleData(object sender, StoreRefreshDataEventArgs e)
        {
            ArticleData = null;
            this.BindArticleData();
        }

        private void BindArticleData()
        {
            this.ArticleStore.DataSource = ArticleData;

            Behaviors.Part p = new Behaviors.Part();
            p.Init(this.PartId);

            this.plPart.Html = string.Format("<p style='padding: 10px 10px 10px 10px; color: #556677; font-size: 15px'>所属栏目：{0}</p>", p.Name);

            this.ArticleStore.DataBind();
        }

        [DirectMethod]
        public int SaveArticle(object obj)
        {
            Ext.Net.JsonReader jo = new JsonReader();
            var article = Newtonsoft.Json.JsonConvert.DeserializeObject(obj.ToString(), typeof(Behaviors.Article)) as Behaviors.Article;
            article.Part = this.PartId;

            if (article.Save()) return article.ID; else return 0;
        }

        [DirectMethod]
        public bool DeleteArticle(object obj)
        {
            Ext.Net.JsonReader jo = new JsonReader();
            var a = Newtonsoft.Json.JsonConvert.DeserializeObject(obj.ToString(), typeof(Behaviors.Article)) as Behaviors.Article;
            return a.Delete();
        }
    }
}